<template>
  <div>
    <!-- 新增订单的对话框 -->
    <el-dialog title="新增订单" :visible.sync="dialogVisible" width="60%">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 账号密码 -->
        <el-form-item label="发货人" prop="sendPersonName">
          <el-input
            type="text"
            v-model="ruleForm.sendPersonName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="发货地址" prop="pass">
          <el-input
            type="text"
            v-model="ruleForm.sendRegion"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <!-- 收货人、收货地址 -->
        <el-form-item label="收货人" prop="receivePersonName">
          <el-input
            type="text"
            v-model="ruleForm.receivePersonName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="收货地址" prop="receiveRegion">
          <el-input
            type="text"
            v-model="ruleForm.receiveRegion"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 货物信息、车辆类型 -->
        <el-form-item label="货物信息" prop="goodsName">
          <el-input
            type="text"
            v-model="ruleForm.goodsName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="选择时间" prop="date">
          <el-date-picker
            v-model="ruleForm.date"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="车辆选择" prop="car">
          <el-select v-model="ruleForm.car" placeholder="请选择车辆">
            <el-option label="小货车" value="小货车"></el-option>
            <el-option label="大货车" value="大货车"></el-option>
            <el-option label="面包车" value="面包车"></el-option>
            <el-option label="卡车" value="卡车"></el-option>
            <el-option label="轻卡车" value="轻卡车"></el-option>
            <el-option label="金杯" value="金杯"></el-option>
            <el-option label="皮卡" value="皮卡"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 编辑订单的对话框 -->
    <el-dialog title="编辑订单" :visible.sync="dialogVisibleTwo" width="60%">
      <el-form
        :model="EditRuleForm"
        status-icon
        :rules="rules"
        ref="EditRuleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <!-- 账号密码 -->
        <el-form-item label="发货人" prop="sendPersonName">
          <el-input
            type="text"
            v-model="EditRuleForm.sendPersonName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="发货地址" prop="pass">
          <el-input
            type="text"
            v-model="EditRuleForm.sendRegion"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <!-- 收货人、收货地址 -->
        <el-form-item label="收货人" prop="receivePersonName">
          <el-input
            type="text"
            v-model="EditRuleForm.receivePersonName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="收货地址" prop="receiveRegion">
          <el-input
            type="text"
            v-model="EditRuleForm.receiveRegion"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- 货物信息、车辆类型 -->
        <el-form-item label="货物信息" prop="goodsName">
          <el-input
            type="text"
            v-model="EditRuleForm.goodsName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="id" prop="_id">
          <el-input
            type="text"
            v-model="EditRuleForm._id"
            autocomplete="off"
          ></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="选择时间" prop="date">
          <el-date-picker
            v-model="EditRuleForm.date"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="车辆选择" prop="car">
          <el-select v-model="EditRuleForm.car" placeholder="请选择车辆">
            <el-option label="小货车" value="小货车"></el-option>
            <el-option label="大货车" value="大货车"></el-option>
            <el-option label="面包车" value="面包车"></el-option>
            <el-option label="卡车" value="卡车"></el-option>
            <el-option label="轻卡车" value="轻卡车"></el-option>
            <el-option label="金杯" value="金杯"></el-option>
            <el-option label="皮卡" value="皮卡"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitEditForm('EditRuleForm')"
            >提交</el-button
          >
          <el-button @click="resetForm('EditRuleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!-- 新增用户的对话框 -->
    <div class="manage-header">
      <el-button @click="dialogVisible = true" type="primary">
        + 新增
      </el-button>
    </div>

    <!-- 表格数据 -->
    <div>
      <el-table
        :data="
          orderInfo.filter(
            (data) =>
              !search ||
              data.sendPersonName.toLowerCase().includes(search.toLowerCase())
          )
        "
        style="width: 100%"
      >
        <!-- <el-table-column label="日期" prop="sendDate1" :formatter="formatter">
        </el-table-column> -->
        <el-table-column label="订单号" prop="_id">
        </el-table-column>
        <el-table-column label="发货人" prop="sendPersonName">
        </el-table-column>
        <el-table-column label="发货地址" prop="sendRegion"> </el-table-column>
        <el-table-column label="收货人" prop="receivePersonName">
        </el-table-column>
        <el-table-column label="收货地址" prop="receiveRegion">
        </el-table-column>
        <el-table-column label="货物信息" prop="goodsName"> </el-table-column>
        <el-table-column label="车辆类型" prop="goodsCar"> </el-table-column>
        <el-table-column align="right">
        <template slot="header" slot-scope="scope">
          <el-input
            v-model="search"
            size="mini"
            placeholder="输入关键字搜索"/>
        </template>
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
import axios from "axios";
const cityOptions = ["上海", "北京", "广州", "深圳", "广西", "其他"];
export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("年龄不能为空"));
      }
      setTimeout(() => {
        if (!Number.isInteger(value)) {
          callback(new Error("请输入数字值"));
        } else {
          if (value < 18) {
            callback(new Error("必须年满18岁"));
          } else {
            callback();
          }
        }
      }, 1000);
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (this.ruleForm.checkPass !== "") {
          this.$refs.ruleForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.pass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      orderInfo: [],
      search: "",
      // 多选框
      checkAll: false,
      // checkedCities: ['上海', '北京','广西'],
      cities: cityOptions,
      isIndeterminate: true,

      // 标签
      // dynamicTags: ['篮球', '健身', '滑板'],
      inputVisible: false,
      inputValue: "",

      // 注册成功后关闭注册对话框
      dialogVisible: false,
      dialogVisibleTwo: false,
      // 获取数据
      ruleForm: {
        sendPersonName: "",
        sendRegion: "",
        receivePersonName: "",
        receiveRegion: "",
        goodsName: "",
        date: "",
        car: "",
      },
      EditRuleForm: {
        sendPersonName: "",
        sendRegion: "",
        receivePersonName: "",
        receiveRegion: "",
        goodsName: "",
        date: "",
        car: "",
        _id:""
      },
      rules: {
        pass: [{ validator: validatePass, trigger: "blur" }],
        checkPass: [{ validator: validatePass2, trigger: "blur" }],
        age: [{ validator: checkAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    handleEdit(value) {
      this.dialogVisibleTwo = true
      this.EditRuleForm.sendPersonName = value.sendPersonName
      this.EditRuleForm.sendRegion = value.sendRegion
      this.EditRuleForm.receivePersonName = value.receivePersonName
      this.EditRuleForm.receiveRegion = value.receiveRegion
      this.EditRuleForm.goodsName = value.goodsName
      this.EditRuleForm._id = value._id
      this.EditRuleForm.car = value.car
    },
    handleDelete(value) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.EditRuleForm._id = value._id
          this.deleteOrderInfo()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      
    },

    // 提交新增订单信息
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          axios
            .post("http://127.0.0.1:3000/orderManage", {
              sendPersonName: this.ruleForm.sendPersonName,
              sendRegion: this.ruleForm.sendRegion,
              receivePersonName: this.ruleForm.receivePersonName,
              receiveRegion: this.ruleForm.receiveRegion,
              goodsName: this.ruleForm.goodsName,
              sendDate1: this.ruleForm.data,
              goodsCar: this.ruleForm.car,
            })
            .then(
              () => {
                this.neworder_success();
                this.dialogVisible = false;
                this.sendOrderInfo()
              },
              () => {
                this.register_failing();
              }
            );
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 提交编辑订单信息
    submitEditForm() {
          axios.post("http://127.0.0.1:3000/orderEditManage", {
              sendPersonName: this.EditRuleForm.sendPersonName,
              sendRegion: this.EditRuleForm.sendRegion,
              receivePersonName: this.EditRuleForm.receivePersonName,
              receiveRegion: this.EditRuleForm.receiveRegion,
              goodsName: this.EditRuleForm.goodsName,
              _id: this.EditRuleForm._id,
              goodsCar: this.EditRuleForm.car,
            })
            .then(
              () => {
                this.EditOrder_success();
                this.dialogVisibleTwo = false;
                this.sendOrderInfo()
              },
              () => {
                this.register_failing();
              }
            );
    },

    // 删除订单信息
    deleteOrderInfo(){
      axios.post("http://127.0.0.1:3000/deleteOrderInfo", {
              _id: this.EditRuleForm._id,
            })
            .then(
              () => {
                this.deleteOrder_success();
                this.sendOrderInfo()
              },
              () => {
                this.register_failing();
              }
            );
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleClose(tag) {
      this.ruleForm.dynamicTags.splice(
        this.ruleForm.dynamicTags.indexOf(tag),
        1
      );
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick(() => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.ruleForm.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
    },
    handleCheckedCitiesChange(value) {
      let checkedCount = value.length;
      this.checkAll = checkedCount === this.cities.length;
      this.isIndeterminate =
        checkedCount > 0 && checkedCount < this.cities.length;
    },
    // 新增订单成功判断
    neworder_success() {
      this.$message({
        showClose: true,
        message: "新增订单信息成功！",
        type: "success",
      });
    },
    // 新增订单成功判断
    EditOrder_success() {
      this.$message({
        showClose: true,
        message: "编辑订单信息成功！",
        type: "success",
      });
    },

    // 删除订单成功判断
    deleteOrder_success() {
      this.$message({
        showClose: true,
        message: "删除订单信息成功！",
        type: "success",
      });
    },
    formatter (row, column) {
            // 获取单元格数据
            let data = row[column.property];
            if (data == null) {
                return null;
            }
            let dt = new Date(data);
            return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate();
        },
    // 注册失败判读
    register_failing() {
      this.$message({
        showClose: true,
        message: "新增订单失败！",
        type: "error",
      });
    },
    sendOrderInfo(){
      axios.post("http://127.0.0.1:3000/orderInfo").then((res) => {
      this.orderInfo = res.data;
    });
    }
  },
  created() {
    this.sendOrderInfo()
  },
};
</script>

<style>
</style>